import axios from 'axios'

import bus from '../../bus'

const api = '/oauth/token';

export default function (username, password, cb, ecb) {
  let data = new FormData();
  data.append('username', username);
  data.append('password', password);
  data.append('grant_type', 'password');
  axios.post(api ,data,
    {
      headers: {
        'Authorization': 'Basic YW5kcm9pZDphbmRyb2lkLXNlY3JldA==',
        'Content-type': 'application/from-data'
      }
    }).then(d => {
      bus.state.token=`${d.data.token_type} ${d.data.access_token}`;
      cb(d.data)
    }).catch(e => ecb(e))
}